package com.sanjiahantang.youjiarole.module.me.contract.preseneters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ListAdapter;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.me.contract.beans.ContractBean;
import com.sanjiahantang.youjiarole.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiarole.module.me.contract.models.impl.ContractModel;
import com.sanjiahantang.youjiarole.module.me.contract.views.ContractView;
import com.sanjiahantang.youjiarole.module.pdf.views.impl.PDFShowActivity;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiarole.module.pdf.views.PdfBaseView.KEY_CONTRACTNAME;

/**
 * Created by zy on 2017/9/8.
 */

public class ContractPresenter extends BaseMvpPresenter<ContractView> implements CallBack<ContractBean> {
    private QuickAdapter<ContractBean.Data> mAdapter;
    private IContractModel contractModel;

    public ContractPresenter() {
        contractModel = new ContractModel();
    }

    //2全部，0待签，1已签
    private String contractType;

    public void loadData(Context context, int type) {
        mView.showProgress();
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, "").toString());
        switch (type) {
            case ContractView.TYPE_ALL:
                param.put("status", "");
                contractType = "2";
                break;
            case ContractView.TYPE_UNSIGNED:
                param.put("status", "0");
                contractType = "0";
                break;
            case ContractView.TYPE_SIGNED:
                param.put("status", "1");
                contractType = "1";
                break;
        }
        contractModel.loadContractList(context, param, this);
    }

    public ListAdapter initAdapter(Context context) {
        return mAdapter = new QuickAdapter<ContractBean.Data>(context, R.layout.item_contract) {
            @Override
            protected void convert(BaseAdapterHelper helper, final ContractBean.Data item) {
                helper.setText(R.id.txtContractName, item.getContractName());
                helper.setText(R.id.txtMoney, "¥ " + item.getContractPrice());
                helper.setText(R.id.txtDesigner, item.getCustomerName());
                helper.setText(R.id.txtState, ContractView.STATE_CN_ARR[Integer.parseInt(item.getStatus())]);
                helper.setText(R.id.txtState, ContractView.STATE_CN_ARR[Integer.parseInt(item.getStatus())]);
                if (Integer.parseInt(item.getStatus()) == 1) {
                    helper.setVisible(R.id.btnOK, true);
                    helper.setText(R.id.btnOK, "查看合同");
                } else {
                    helper.setVisible(R.id.btnOK, false);

                }
                helper.setOnClickListener(R.id.btnOK, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, PDFShowActivity.class);
                        intent.putExtra(StaticValue.KEY_CONTRACT, item.getContractCode());
                        intent.putExtra(ContractView.KEY_CONTRACT_TYPE, contractType);
                        intent.putExtra(KEY_CONTRACTNAME, item.getContractName());

                        ((Activity) context).startActivityForResult(intent, 3);
                    }
                });
                helper.setVisible(R.id.txtRemain, true);
                helper.setText(R.id.txtRemain, item.getStatusName());
            }
        };
    }

    @Override
    public void onSuccess(ContractBean response, Context context, Object tag) {
        if (mView == null) return;
        hideProgress();
        if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
            mAdapter.clear();
            mAdapter.addAll(response.getData());
        } else {
            Utils.showToast(context, "合同列表获取失败");
        }

    }

    private void hideProgress() {
        if (mView != null) mView.hideProgress();
    }

    @Override
    public void onFailure(String message) {
        if (mView == null) return;
        hideProgress();
    }


}
